package com.huawei.Galenus.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.Galenus.domain.Message;
import com.huawei.Galenus.domain.Proposal1;
import com.huawei.Galenus.service.FabricService;
import com.huawei.Galenus.service.ProposalService;
import com.huawei.Galenus.service.UserProposalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("/api/proposal")
@Api("提案相关接口")
public class ProposalController {

    @Autowired
    private ProposalService proposalService;

    @Autowired
    private FabricService fabricService;

    @Autowired
    private UserProposalService userProposalService;

    @ApiOperation("发起提案")
    @PostMapping("/launch")
    public Message launch(@RequestBody Proposal1 proposal1) {
        String id = UUID.randomUUID().toString();
        proposal1.setId(id);
        JSONObject jsonObject = (JSONObject) JSON.toJSON(proposal1);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "proposal.createTx";
        JSONObject payload = fabricService.ssaInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            return new Message().ok(6666, "创建成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "创建失败").addData("payload", payload);
        }
    }

    @ApiOperation("查询某个提案")
    @GetMapping("/query")
    public Message query(@RequestParam("proposalID") String proposalID) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("proposalID", proposalID);
        jsonObject.put("type", "1");
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "proposal.query";
        JSONObject payload = fabricService.ssaQuery(method, params);
        if (payload.getString("wrong_msg") == null) {
            return new Message().ok(6666, "查询成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "查询失败").addData("payload", payload);
        }
    }

    @ApiOperation("投票")
    @PostMapping("/vote")
    public Message vote(@ApiParam("提案ID") @RequestParam("pid") String pid,
                        @ApiParam("用户ID") @RequestParam("uid") String uid,
                        @ApiParam("选项") @RequestParam("choice") String choice,
                        @ApiParam("投票数") @RequestParam("amount") int amount) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("proposalID", pid);
        jsonObject.put("accountID", uid);
        jsonObject.put("choice", choice);
        jsonObject.put("amount", amount);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "proposal.voteTx";
        JSONObject payload = fabricService.ssaInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            return new Message().ok(6666, "投票成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "投票失败").addData("payload", payload);
        }
    }
}
